﻿HmJS.$Import('core.function');

HmJS.register('base.common', function ($) {

	// #region -- Request --

	(function () {

		/*
		自己扩展的,用于响应输出为XML的Ajax请求类
		主要是为返回的xml对象附加支持XPath的selectNodes和selectSingleNode方法
		因为IE本身已经支持这两方法,所以主要是针对支持XPath的其它浏览器
		document.evaluate方法的用法可参考以下网址:
		http://developer.mozilla.org/en/docs/DOM:document.evaluate
		http://wiki.mozcn.org/index.php/Firefox:Dive_Into_Greasemonkey/4.6._Doing_something_for_every_element_with_a_certain_attribute
		*/
		Request.XML = new Class({

			//继承自Request
			Extends: Request,

			//请求成功,覆盖Request类的同名方法
			success: function (text, xml) {
				//因为IE本身已经支持这两方法,所以主要是针对支持XPath的其它浏览器
				if (Browser.Features.xpath) {
					//扩展实现selectNodes方法
					xml.selectNodes = function (xpath) {
						var nodes = [], node;
						//几个参数的意义
						var result = this.evaluate(xpath, this, this.createNSResolver(this.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
						if (result) while (node = result.iterateNext()) nodes.push(node);
						return nodes;
					};
					//扩展实现selectSingleNode方法
					xml.selectSingleNode = function (xpath) {
						var result = this.evaluate(xpath, this, this.createNSResolver(this.documentElement), 9, null);
						return (result && result.singleNodeValue) ? result.singleNodeValue : [];
					};
				}
				//调用父类方法,触发成功事件
				this.onSuccess(xml, text);
			}
		});

	})();

	// #endregion

	// #region -- Locale.AutoUse --

	/*
	---
	name: Locale.AutoUse
	version: 0.2-0
	description: Automatically detects the Language on the page
	download: http://mootools.net/forge/p/locale_autouse
	source: http://github.com/arian/Locale.AutoUse

	authors: Arian Stolwijk

	requires: [Core/DOMReady, Core/Element, More/Locale]
	provides: Locale.AutoUse
	...
	*/

	Locale.AutoUse = function (selector, attr, ua) {

		var languages = Locale.list(), lang;

		if (selector == navigator) { ua = true; }

		if (!ua) {
			if (!selector) { selector = '*[lang]'; }
			if (!attr) { attr = 'lang'; }

			var element = document.getElement(selector);
			if (!element) { return Locale; }

			lang = element.get(attr);
		} else {
			lang = navigator.language || navigator.userLanguage;
		}

		if (!languages.contains(lang)) {
			for (var i = 0, l = languages.length; i < l; i++) {
				var index = languages[i].indexOf('-'),
						firstPart = languages[i].substr(0, index);

				if (firstPart == lang) {
					lang = languages[i];
					break;
				}
			}
		}

		return Locale.use(lang);

	};

	// #endregion

	// #region --  --

	(function () {
	})();

	// #endregion

	// #region --  --

	(function () {
	})();

	// #endregion

	// #region --  --

	(function () {
	})();

	// #endregion

});
